﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using QuanLyBanHang.QuerySQL;

namespace QuanLyBanHang
{
    public partial class frNguoidung : Office2007Form
    {
        ClassNguoidung us = new ClassNguoidung();
        DataTable tbUser = new DataTable();
        bool ts;
        String tk;
        bool ktra;
        public frNguoidung()
        {
            InitializeComponent();
        }
        //
        public void Xulybutton(bool b)
        {
            this.btnthem.Enabled = btnsua.Enabled = b;
            btnLuu.Enabled = btnhuy.Enabled = panelEx_txt.Enabled = !b;
        }

        //
        private void frNguoidung_Load(object sender, EventArgs e)
        {
            Xulybutton(true);
            tbUser = us.ShowUser();
            dtgv_nguoidung.DataSource = tbUser;
            
        }

        private void dtgv_nguoidung_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            try
            {
                for (int i = 0; i < dtgv_nguoidung.RowCount; i++)
                {
                    dtgv_nguoidung.Rows[i].Cells[0].Value = i + 1;
                }
            }
            catch {  }
        }

        private void btnthem_Click(object sender, EventArgs e)
        {
            ts = true;
            txttaikhoan.Clear();
            cbonhanvien.Text = "";
            cboquyen.Text = "";
            txtpass.Clear();
            Xulybutton(false);
        }

        private void btnsua_Click(object sender, EventArgs e)
        {
            ts = false;
            Xulybutton(false);
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (this.txttaikhoan.TextLength == 0)
            {
                lbthongbao.Text = "* Bạn chưa nhập tài khoản...";
                txttaikhoan.Focus();
            }
            else
            {
                if (cbonhanvien.Text == "")
                {
                    lbthongbao.Text = "* Hãy Chọn nhân viên...";
                    cbonhanvien.Focus();
                }
                else
                {
                    if (cboquyen.Text.Equals(""))
                    {
                        lbthongbao.Text = "* Hãy Chọn quyền...";
                        cboquyen.Focus();
                    }
                    else
                    {
                        if (txtpass.TextLength == 0)
                        {
                            lbthongbao.Text = "* Chưa nhập mật khẩu...";
                            txtpass.Focus();
                        }
                        else
                        {                            
                            
                            if (ts == false)
                            {
                                us.Suatk(tk,txttaikhoan.Text,cboquyen.Text.ToString(),cbonhanvien.Text.ToString(),txtpass.Text);
                                frNguoidung_Load(sender, e);
                                lbthongbao.Text = "*";
                                Xulybutton(true);
                            }
                            else
                            {
                                if (!ktra)
                                {
                                    us.themnguoidung(txttaikhoan.Text, cboquyen.Text.ToString(), cbonhanvien.Text.ToString(), txtpass.Text);
                                    frNguoidung_Load(sender, e);
                                    lbthongbao.Text = "";
                                    Xulybutton(true);
                                }
                                else
                                {
                                    MessageBox.Show("Tài khoản đã tồn tại !!!");

                                }
                            }
                        }
                    }
                }
            }
        }

        private void btnhuy_Click(object sender, EventArgs e)
        {
            frNguoidung_Load(sender, e);
            Xulybutton(true);
        }

        private void btnxoa_Click(object sender, EventArgs e)
        {
            if (btnthem.Enabled)
            {
                DialogResult kq = MessageBox.Show("Bạn có chắc muốn xóa thài khoản " + txttaikhoan.Text + "  ? ", "Thông báo", MessageBoxButtons.YesNo);
                if (kq == DialogResult.Yes)
                {

                    us.Xoanguoidung(tk);
                    frNguoidung_Load(sender, e);
                }
            }
            else
            {
                MessageBox.Show("Hãy thực hiện lưu dữ liệu !!!");
            }
        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void dtgv_nguoidung_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int dong = e.RowIndex;
                this.txttaikhoan.Text = tk = dtgv_nguoidung.Rows[dong].Cells[1].Value.ToString();
                this.cbonhanvien.Text = dtgv_nguoidung.Rows[dong].Cells[3].Value.ToString();
                this.cboquyen.Text = dtgv_nguoidung.Rows[dong].Cells[2].Value.ToString();
                this.txtpass.Text = dtgv_nguoidung.Rows[dong].Cells[4].Value.ToString();
            }
            catch {   }
        }

        private void buttonItem1_Click(object sender, EventArgs e)
        {
            frNguoidung_Load(sender, e);
        }

        private void textBoxX1_TextChanged(object sender, EventArgs e)
        {
            dtgv_nguoidung.DataSource = us.Timkiem(txttimkiem.Text);
        }

        private void txttaikhoan_TextChanged(object sender, EventArgs e)
        {
            ktra = false;
            foreach (DataRow row in tbUser.Rows)
            {
                if (row["ID"].ToString().Equals(txttaikhoan.Text))
                {
                    ktra = true;
                }
            }
        }

        private void cbonhanvien_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        private void cbonhanvien_Click(object sender, EventArgs e)
        {
            cbonhanvien.DataSource = us.ShowMANV();
            cbonhanvien.ValueMember = "MANV";
            cbonhanvien.DisplayMember = "MANV";
        }
    }
}
